package com.treasure.domain.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.treasure.web.AiTreasureChestApplication;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Types;
import java.util.Collections;

/**
 * @author sunhongmin
 * @date 2023/8/31 19:57
 * @description
 */
@SpringBootTest(classes = AiTreasureChestApplication.class)
@RunWith(SpringRunner.class)
@Profile(value = "pgvector")
public class MpMybatisPlusGeneratorUtil {
    @Test
    public void sqlGenerate(){
        FastAutoGenerator.create("jdbc:postgresql://localhost:5432/postgres", "postgres", "password")
                .globalConfig(builder -> {
                    builder.author("sunhongmin") // 设置作者
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("/Users/sunhongmin/IdeaProjects/ai-treasure-chest/treasure-infrastructure/src/main/java/com/treasure/infrastructure/repository"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.treasure.infrastructure.repository") // 设置父包名
                            .moduleName("treasure-infrastructure") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "/Users/sunhongmin/IdeaProjects/ai-treasure-chest/treasure-infrastructure/src/main/resources/mapper\n")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("corp_info")
                            .addInclude("corp_user_info")// 设置需要生成的表名
                            .addInclude("account")// 设置需要生成的表名
                            .addInclude("corp_role_info")// 设置需要生成的表名
                            .addInclude("user_role_mapping")// 设置需要生成的表名
                            .addInclude("knowledge_base")// 设置需要生成的表名
                            .addInclude("knowledge_vector")// 设置需要生成的表名
                            .addInclude("ai_robot")// 设置需要生成的表名
                            .addInclude("robot_setting")// 设置需要生成的表名
                            .addInclude("interaction_history")// 设置需要生成的表名
                            .addInclude("chat_history")// 设置需要生成的表名
                            .addInclude("corp_department_info")// 设置需要生成的表名
                    ; // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
